package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 构建查询条件  name like
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        wrapper.eq(Dish::getStatus, 1);

        //2. 构建分页条件  page
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //3. 执行根据name分页查询菜品信息列表
        page = dishMapper.selectPage(page, wrapper);
        //遍历上面的列表，得到每一个菜品对象
        List<Dish> dishList = page.getRecords();

        if (CollectionUtils.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品category_id查询当前菜品分类
                Category category = categoryMapper.selectById(dish.getCategoryId());
                String categoryName = category.getName();
                dish.setCategoryName(categoryName);


                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }

        return page;
    }

    @Transactional//事务
    @Override
    public void save(Dish dish) {
        //1.将dish基本信息保存到菜品表中
        dishMapper.insert(dish);
        //这个位置要获取到刚刚插入的菜品的主键，这是为了给口味表的外键的dish.getId()

        //2.将dish中的口味遍历保存到口味表中
        for (DishFlavor flavor : dish.getFlavors()) {
            if (CollectionUtils.isNotEmpty(dish.getFlavors())) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);//这里会有事务问题
                //同一业务中，发送多条增删改语句的时候，一定要注意事务问题
            }

        }
    }

    @Override
    public Dish findById(Long id) {
        //查询菜品还有菜品对应的口味表信息
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);//封装口味列表
        return dish;
    }

    @Transactional
    @Override
    public void update(Dish dish) {
        dishMapper.updateById(dish);
        //2根据菜品id山出口表中相关口味，然后想口味加入改变后的的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);
        //重新添加
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtils.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //删除之前先判断是否是停售状态
        //select count(0) from dish where id in #{id} and status=1
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(Dish::getId, ids);
        wrapper1.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(wrapper1);
        if (count > 0) {
            throw new CustomException("要删除的菜品存在在售状态，不能删除");
        }
//到这里说明是停售状态的，可以删除
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.deleteById(wrapper);
            dishMapper.deleteById(id);
        }


    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        for (Long id : ids) {
//            Dish dish = dishMapper.selectById(id);
//            dish.setStatus(status);
//            dishMapper.updateById(dish);
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
            //这种写法更简单，因为他支持动态sql，不用查询
        }
    }

    @Transactional
//    @Override
//    public List<Dish> findList(Long categoryId, Integer status) {
//        //1. 设置查询条件
//        //select * from dish where category_id = ? and status = 1
//        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Dish::getCategoryId, categoryId);
//        wrapper.eq(Dish::getStatus, status);
//
//        //2. 执行查询  菜品列表
//        List<Dish> dishList = dishMapper.selectList(wrapper);
//
//        //3. 遍历这个列表
//        if (CollectionUtils.isNotEmpty(dishList)) {
//            for (Dish dish : dishList) {
//                //4. 得到每个菜品, 根据菜品的id从口味表找到当前菜品的口味列表
//                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
//                wrapper1.eq(DishFlavor::getDishId,dish.getId());
//                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
//                dish.setFlavors(dishFlavorList);
//            }
//        }
//
//        return dishList;
//    }
    @Override
    public List<Dish> findList(Long categoryId, Integer status,String name) {
        //1. 设置查询条件
        //select * from dish where category_id = ? and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        if (categoryId!=null){
            wrapper.eq(Dish::getCategoryId, categoryId);
        }

        wrapper.eq(Dish::getStatus, status);
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);

        //2. 执行查询  菜品列表
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //3. 遍历这个列表
        if (CollectionUtils.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //4. 得到每个菜品, 根据菜品的id从口味表找到当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }

        return dishList;
    }

}

